package com.lsa.letcode.common;

import static org.junit.Assert.assertTrue;

import java.util.Objects;

public class LetCodeTestBase {
	protected ListNode asList(int... arr) {
		if (arr.length == 0) {
			return null;
		}
		ListNode first = new ListNode(arr[0]);
		ListNode prev = first;
		for (int i = 1; i < arr.length; i++) {
			ListNode cur = new ListNode(arr[i]);
			prev.next = cur;
			prev = cur;
		}
		return first;
	}
	
	protected int[] toArray(ListNode n) {
		int size = size(n);
		int[] arr = new int[size];
		int i = 0;
		while(n != null) {
			arr[i++] = n.val;
			n = n.next;
		}
		return arr;
	}
	
	protected int size(ListNode head) {
		int size = 0;
		while(head != null) {
			size++;
			head = head.next;
		}
		return size;
	}
	
	protected static boolean areEqual(ListNode l1, ListNode l2) {
		if (l1 == null) {
			return l2 == null;
		}
		
		ListNode n1, n2 ;
		for(n1 = l1, n2 = l2; n1 != null && n2 != null; n1 = n1.next,n2 = n2.next) {
			if (!Objects.equals(n1, n2)) {
				return false;
			}
		}
		
		return n1 == n2;
	}
	
	protected void assertEqual(ListNode expected, ListNode actual) {
		assertTrue(String.format("expected:%s, actual:%s", expected, actual), areEqual(expected, actual));
	}
	
	protected void print(int[][] m) {
		for (int i = 0; i < m.length; i++) {
			for (int j = 0; j < m[i].length; j++) {
				System.out.print(String.format("%2s ", m[i][j]));
			}
			System.out.println();
		}
	}
}
